package com.shengde.jinwawaforleader.model.myJob.classStatus;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.network.BasexUtilsProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProLookPeopleNum;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * 相册 观看过的人
 * Created by Administrator on 2016/12/30 0030.
 */
public class LookPeopleNumActivity extends BaseActivity {
	private static final String TAG = "LookPeopleNumActivity";
	private static final int FINSH = 1;
	private TextView tv_back;
	private TextView tv_title;
	private ListView lv_look_people;
	private int joinid;
	private List<ProLookPeopleNum.DataBean> dataBean;
	private CommonBaseAdapter commonBaseAdapter;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (commonBaseAdapter == null) {
						commonBaseAdapter = new CommonBaseAdapter(LookPeopleNumActivity.this, dataBean, R.layout.lookpeople_item) {
							@Override
							public void convert(ViewHolder holder, int position) {
								int flag = dataBean.get(position).getFlag();
								Log.i(TAG, "convert: flag= " + flag);
								if ("1".equals(flag) || 1 == flag) {

									holder.setText(R.id.tv_lookpeople_name, dataBean.get(position).getName() + "(学生)");
								} else {
									holder.setText(R.id.tv_lookpeople_name, dataBean.get(position).getName() + "(老师)");

								}
							}
						};
						lv_look_people.setAdapter(commonBaseAdapter);
					} else {
						commonBaseAdapter.notifyDataSetChanged();
					}
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.activity_look_people_num);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		lv_look_people = (ListView) findViewById(R.id.lv_look_people);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.look_people_num));
		joinid = getIntent().getIntExtra("joinid", 0);
		Log.i(TAG, "showLookPeopleNum: joinid = " + joinid);
		showLookPeopleNum();
	}

	/**
	 * 获取浏览的人的记录
	 */
	private void showLookPeopleNum() {
		RequestParams params = BasexUtilsProtocol.sendHttpRequest(this, "/teacher/recordList");
		params.addParameter("type", 1);
		params.addParameter("albumid", joinid);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProLookPeopleNum mPro_look_people_num = new Gson().fromJson(result, ProLookPeopleNum.class);
				Log.i(TAG, "onSuccess: code = " + mPro_look_people_num.getCode());
				if (mPro_look_people_num != null && mPro_look_people_num.getCode() == 0) {
					dataBean = mPro_look_people_num.getData();
					mHandler.sendEmptyMessage(FINSH);
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				Log.i(TAG, "onError: ex = " + ex);

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {
				BasexUtilsProtocol.dialog.cancel();
			}
		});
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
		}
	}
}
